package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FastDFSUtil;
import com.ruoyi.system.domain.SysAnnounManagement;
import com.ruoyi.system.domain.mongodb.Schools;
import com.ruoyi.system.mapper.SysAnnounManagementMapper;
import com.ruoyi.system.service.ISysAnnounManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 大赛公告管理 服务层处理
 *
 * @author ganshiyu
 */
@Service
public class SysAnnounManagementServiceImpl implements ISysAnnounManagementService {
     @Autowired
     SysAnnounManagementMapper sysAnnounManagementMapper;
     @Autowired
     FastDFSUtil fastDFSUtil;
     @Autowired
     SysEventsPostedServiceImpl sysEventsPostedService;

     /**
      * 大赛公告管理  条件查询+分页
      * 后台管理和校园管理平台共同使用
      * @param sysAnnounManagement 实体类
      * @return 对象列表
      */
     @Override
     public Map<String, Object> selectByPage(SysAnnounManagement sysAnnounManagement) {
          Map<String,Object> data=new HashMap<>();

          //获取教师id
          String tid = sysAnnounManagement.getTid();
          //当教师id不为空时 （校园管理平台中会使用）
          if (tid!=null){
               //获取学校学校
               Schools schools = sysEventsPostedService.queryUsers(tid);
               //获取学校名字
               String names = schools.getNames();
               //封装
               sysAnnounManagement.setSchoolName(names);
          }

          //查数据
          List<SysAnnounManagement> stus= sysAnnounManagementMapper.selectByPage(sysAnnounManagement);

          //查数目
          long total = sysAnnounManagementMapper.selectCount(sysAnnounManagement);

          data.put("stus",stus);
          data.put("total",total);

          return data;
     }

     /**
      * 大赛公告添加
      * 后台管理和校园管理平台共同使用
      * @param sysAnnounManagement 实体类
      * @return 结果
      */
     @Override
     public int insertAnnounManagement(SysAnnounManagement sysAnnounManagement) {
          //获取教师id
          String tid = sysAnnounManagement.getTid();
          //当教师id不为空时 （校园管理平台中会使用）
          if (tid!=null){
               //获取学校学校
               Schools schools = sysEventsPostedService.queryUsers(tid);
               //获取学校名字
               String names = schools.getNames();
               //封装`
               sysAnnounManagement.setSchoolName(names);
          }


          String filepath="";
               //获取文件对象
               MultipartFile fileile = sysAnnounManagement.getAmPicFile();
          try {
               if (fileile.getSize()!=0) {
                    //上传文件
                    filepath = fastDFSUtil.uploadFile(fileile);
                    System.out.println("filepath：" + filepath);
                    //获取文件全路径
                    String url = fastDFSUtil.getResAccessUrl(filepath);
                    //封装对象
                    sysAnnounManagement.setAmPic(url);
               }
          } catch (Exception ioException) {
               ioException.printStackTrace();
          }
          //查询数据是否存在
          long exists = sysAnnounManagementMapper.exists(sysAnnounManagement);
          int i=0;
          //不存在再添加
          if (exists==0){
               i= sysAnnounManagementMapper.insertAnnounManagement(sysAnnounManagement);
          }
          return i;
     }

     /**
      *  修改大赛公告管理状态
      *  后台管理和校园管理平台共同使用
      * @param amid
      * @return 结果
      */
     @Override
     public int editState(int amid) {
          int i = sysAnnounManagementMapper.editState(amid);
          return i;
     }
     /**
      *  通过id查询对象
      *  后台管理和校园管理平台共同使用
      * @param amid
      * @return 对象
      */
     @Override
     public SysAnnounManagement selectById(int amid) {
          SysAnnounManagement sysAnnounManagement = sysAnnounManagementMapper.selectById(amid);
          return sysAnnounManagement;
     }
     /**
      * 修改大赛公告
      * 后台管理和校园管理平台共同使用
      * @param sysAnnounManagement 实体类
      * @return 结果
      */
     @Override
     public int edit(SysAnnounManagement sysAnnounManagement) {
          //获取文件对象
           MultipartFile amPicFile = sysAnnounManagement.getAmPicFile();
          String filepath="";
          int i=0;
          try {
               //如果用户重新上传图片文件
               if(amPicFile.getSize()!=0){
                    //删除原有的图片文件
                     Boolean aBoolean = fastDFSUtil.deleteFile(sysAnnounManagement.getAmPic());
                     if(aBoolean==true || StringUtils.isEmpty(sysAnnounManagement.getAmPic())){
                          System.out.println("删除成功");
                          //上传文件
                          filepath = fastDFSUtil.uploadFile(amPicFile);
                          //获取文件全路径
                          String url = fastDFSUtil.getResAccessUrl(filepath);
                          //封装对象
                          sysAnnounManagement.setAmPic(url);
                     }else {
                          System.out.println("删除失败");
                     }
               }
          } catch (IOException e) {
               e.printStackTrace();
          }
          i = sysAnnounManagementMapper.edit(sysAnnounManagement);
          return i;
     }

     /**
      *  删除大赛公告
      *  后台管理和校园管理平台共同使用
      * @param amid
      * @return 结果
      */
     @Override
     public int remove(int amid) {
          //根据id查询对象
          SysAnnounManagement sysAnnounManagement = sysAnnounManagementMapper.selectById(amid);
          //获取图片
          String amPic = sysAnnounManagement.getAmPic();
          int i=0;
          if (amPic!=null){
               System.out.println(1);
               //1.有图片的时候删除图片和数据
               //删除原有图片
               Boolean aBoolean = fastDFSUtil.deleteFile(amPic);
               if (aBoolean==true){
                    //删除数据
                    i= sysAnnounManagementMapper.remove(amid);
               }
          }else {
               //2.没图片的时候，删除数据
               i= sysAnnounManagementMapper.remove(amid);
          }
          return i;
     }

     /*
      * 查询大赛公告标题
      * */
     @Override
     public List<SysAnnounManagement> selectAmTitle() {
          List<SysAnnounManagement> sysAnnounManagements = sysAnnounManagementMapper.selectAmTitle();
          return sysAnnounManagements;
     }

     /*
      * 查询大赛公告状态
      * */
     @Override
     public List<SysAnnounManagement> selectAmState() {
          List<SysAnnounManagement> sysAnnounManagements = sysAnnounManagementMapper.selectAmState();
          return sysAnnounManagements;
     }

    @Override
    public Map<String,Object> readAnnounMans(SysAnnounManagement sysAnnounManagement) {
         Map<String,Object> map = new HashMap<>();
         map.put("sysAnnounManagement", sysAnnounManagementMapper.readAnnounMans(sysAnnounManagement));
         map.put("total", sysAnnounManagementMapper.readAnnounManTotal());
         return map;
    }
}
